package com.gxa.autoparts.admin.service.impl;

import com.gxa.autoparts.admin.mapper.AdminMapper;
import com.gxa.autoparts.admin.service.AdminService;
import com.gxa.autoparts.core.pojo.Admin;
import com.gxa.autoparts.core.pojo.Permission;
import com.gxa.autoparts.core.pojo.Role;
import com.gxa.autoparts.core.service.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service("adminService")
public class AdminServiceImpl extends BaseServiceImpl<Admin> implements AdminService {
    @Resource
    private AdminMapper adminMapper;

    @Override
    public List<Map> selectAllAdmin(Integer page, Integer size) {
        return adminMapper.selectAllAdmin(page, size);
    }

    @Override
    public Admin selectByName(String name) {
        return adminMapper.selectByName(name);
    }

    @Override
    public List<Role> getRoleList(String name) {
        return adminMapper.getRoleList(name);
    }

    @Override
    public List<Permission> getPermissionList(Long id) {
        List<Permission> permissionList = adminMapper.getPermissionList(id);
        List<Permission> permissions = new ArrayList<>();
        for (Permission permission:permissionList){
            if (permission.getPid().equals(0L)){
                permissions.add(permission);
                /*for(Permission permission1:permissionList){
//                    比较权限的id和当前找到的权限的id相对
                    if (permission1.getPid().equals(permission.getId())){
                        permission.getChildren().add(permission1);
                    }
                }*/
                getChildren(permissionList,permission);
            }
        }
        return permissions;
    }

    @Override
    public List<Map> selectAllAdmin2() {
        return adminMapper.selectAllAdmin2();
    }

    @Override
    public List<Permission> selectPermissionByName(String name) {
        List<Permission> permissionList = adminMapper.selectPermissionByName(name);
        List<Permission> permissions = new ArrayList<>();
        for (Permission permission:permissionList){
            if (permission.getPid().equals(0L)){
                permissions.add(permission);
                /*for(Permission permission1:permissionList){
//                    比较权限的id和当前找到的权限的id相对
                    if (permission1.getPid().equals(permission.getId())){
                        permission.getChildren().add(permission1);
                    }
                }*/
                getChildren(permissionList,permission);
            }
        }
        return permissions;
    }

    public void getChildren(List<Permission> permissionList,Permission permission1){
        for (Permission permission:permissionList){
            if (permission.getPid().equals(permission1.getId())){
                permission1.getChildren().add(permission);
                getChildren(permissionList,permission);
            }
        }
        return;
    }
}
